package com.ruitu.mall_98.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.ScreenUtils;
import com.beanu.arad.utils.SizeUtils;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98.R;
import com.ruitu.mall_98_1.product.activity.ProductListByModelActivity;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.bean.app.BannerNew;
import com.ruitu.router_module.bean.app.HomeModel;
import com.ruitu.router_module.bean.product.Product;
import com.ruitu.router_module.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

public class ModelAdapter extends ListBaseAdapter<List<HomeModel>> {
    public List<BannerNew> modelBanner = new ArrayList<>();

    public ModelAdapter(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.app_item_home_model;
    }

    //模块类型（2，淘一淘；3，逛一逛；4，吃货街；5，厂家清仓；6，地方特产；7，行业精品；8，推荐商品，9，家居小世界）
    private String[] nameArr = {"地方特产", "淘一淘", "逛一逛", "吃货街", "厂家清仓", "行业精品", "推荐商品", "家居小世界"};

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int pos) {
        TextView tv_name = holder.getView(R.id.tv_name);
        TextView tv_more = holder.getView(R.id.tv_more);
        ImageView iv_title = holder.getView(R.id.iv_title);
        RecyclerView rcv_pro = holder.getView(R.id.rcv_pro);

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) iv_title.getLayoutParams();
        params.width = ScreenUtils.getScreenWidth();
        params.height = params.width * 240 / 810;//banner的比例是810 * 240
        iv_title.setLayoutParams(params);

        final List<HomeModel> modelProList = mDataList.get(pos);

        try {
            tv_name.setText(nameArr[pos]);
            BannerNew bannerNew = modelBanner.get(pos);
            if (null == bannerNew || TextUtils.isEmpty(bannerNew.getId())
                    || TextUtils.isEmpty(bannerNew.getImgApp())) {
                iv_title.setVisibility(View.GONE);//没有数据的时候隐藏banner(特色专场)
            } else {
                iv_title.setVisibility(View.VISIBLE);
                GlideUtil.loadImg(Config.BASE_URL + bannerNew.getImgApp(), iv_title);
            }
        } catch (Exception e) {
            iv_title.setVisibility(View.GONE);//没有数据的时候隐藏banner(特色专场)
            e.printStackTrace();
        }

        ModelProAdapter proAdapter = new ModelProAdapter(mContext);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcv_pro.setLayoutManager(manager);
        rcv_pro.setAdapter(proAdapter);
        proAdapter.setDataList(modelProList);
        proAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                try {
                    Bundle bundle = new Bundle();
                    Product product = new Product();
                    product.setGoodId(modelProList.get(position).getGoodId());
                    bundle.putSerializable("product", product);
                    A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

        });

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOnclick(modelProList);
            }
        };
        tv_more.setOnClickListener(onClickListener);
        tv_name.setOnClickListener(onClickListener);
        iv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(mContext, ProductListByModelActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("type", modelBanner.get(pos).getId());
                    bundle.putString("model_name", modelBanner.get(pos).getName());
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                } catch (Exception e) {
                    ToastUtils.showShort("暂无商品");
                }
            }
        });
    }

    private void setOnclick(final List<HomeModel> modelProList) {
        try {
            Intent intent = new Intent(mContext, ProductListByModelActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("type", modelProList.get(0).getType());
            intent.putExtras(bundle);
            mContext.startActivity(intent);
        } catch (Exception e) {
            ToastUtils.showShort("暂无商品");
        }
    }

    //八大模块商品的adapter
    public class ModelProAdapter extends ListBaseAdapter<HomeModel> {

        public ModelProAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.app_item_home_horizontal_pro;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int pos) {
            ImageView icon = holder.getView(R.id.icon);//商品图片
            TextView tv_name = holder.getView(R.id.tv_name);//商品名称
            TextView tv_price = holder.getView(R.id.tv_price);//商品价格

            int width = ScreenUtils.getScreenWidth();
//            int itemWidth = (width - (SizeUtils.dp2px(20))) / 2;
            int itemWidth = SizeUtils.dp2px(120);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
            params.width = itemWidth;
            params.height = itemWidth;
            icon.setLayoutParams(params);

            HomeModel model = mDataList.get(pos);
            GlideUtil.loadImg(Config.BASE_URL + model.getImageUrl(), icon);
            Product pro = model.getGood();
            tv_price.setText("￥" + pro.getOriginalPrice());
            tv_name.setText(model.getGoodName());

            TextView tv_sale_num = holder.getView(R.id.tv_sale_num);//销量
            if (null != tv_sale_num) {
                tv_sale_num.setText("销量" + (pro.getGoodVolume() > 999 ? 999 + "+" : pro.getGoodVolume() + ""));
            }

            //下面是设置标签的,只适用于部分页面
            TextView tag_zidingyi = holder.getView(R.id.tag_zidingyi);//自定义
            TextView tag_pintuan = holder.getView(R.id.tag_pintuan);//拼团
            TextView tag_qujianjia = holder.getView(R.id.tag_qujianjia);//区间价
            TextView tag_yuding = holder.getView(R.id.tag_yuding);//预订
            TextView tag_yushou = holder.getView(R.id.tag_yushou);//预售

            try {
                if (!TextUtils.isEmpty(pro.getGoodTagName())) {//是否有自定义标签
                    tag_zidingyi.setVisibility(View.VISIBLE);
                    tag_zidingyi.setText(pro.getGoodTagName());//设置自定义标签名称
                } else {
                    tag_zidingyi.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getShoppingGroupStatus())) {//是否团购 0 否 1 是
                    tag_pintuan.setVisibility(View.VISIBLE);
                } else {
                    tag_pintuan.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getIntervalStatus())) {//是否区间价格 0 否 1 是
                    tag_qujianjia.setVisibility(View.VISIBLE);
                } else {
                    tag_qujianjia.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getCustomizedStatus())) {//是否预订 0 否 1 是
                    tag_yuding.setVisibility(View.VISIBLE);
                } else {
                    tag_yuding.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getGoodSalesModel())) {//销售模式（1：预售、2：现货)
                    tag_yushou.setVisibility(View.VISIBLE);
                } else {
                    tag_yushou.setVisibility(View.INVISIBLE);
                }
            } catch (Exception e) {
            }
        }
    }
}
